﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

namespace G.Data
{
    public sealed class Table<T> : ObservableCollection<T>
    {
        internal Table()
        {

        }

        protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            base.OnCollectionChanged(e);
            IsChanged = true;
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add || e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace)
            {
                foreach (var item in e.NewItems)
                {
                    IKey key = item as IKey;
                    if (key != null)
                    {
                        if (key.Key == 0)
                            key.Key = DateTime.Now.GetTimestamp();
                    }
                }

                foreach (INotifyPropertyChanged item in e.NewItems)
                {
                    if (item != null)
                        item.PropertyChanged += item_PropertyChanged;
                }
            }
        }

        internal bool IsChanged;

        void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            IsChanged = true;
        }
    }
}
